package com.ishitong.freesql.mapping;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.type.Type;

/**
 * @功能描述: hibernate 与java类型转换工具类
 * @开发人员: liufeng
 * @创建日期: Aug 22, 2013 3:41:06 PM
 */
public class HbmTypeTransHelper {

  /**
   * hibernate 类型名称与java类型名称对应
   */
  static final Map<String, List<String>> textHbmTypeToJavaTypeMap = new HashMap<String, List<String>>();

  /**
   * hibernate 类型字符名称与 hibernate 类型名称对应
   */
  static final Map<String, Type>         textHbmTypeToHbmTypeMap  = new HashMap<String, Type>();

  /**
   * @功能描述: 根据hbmType名称，返回可转换的java类型列表
   * @param textHbmType
   * @return
   * @异常类型:
   */
  public static List<String> transJavaType(String textHbmType) {
    if (textHbmTypeToJavaTypeMap.containsKey(textHbmType)) {
      return textHbmTypeToJavaTypeMap.get(textHbmType);
    }
    List<String> javaTypeList = new ArrayList<String>();
    if (textHbmType.equals("integer")) {
      javaTypeList.add("java.lang.Integer");
      javaTypeList.add("int");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }else if(textHbmType.equals("biginteger")){
      javaTypeList.add("java.math.BigInteger");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("long")) {
      javaTypeList.add("java.lang.Long");
      javaTypeList.add("long");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("short")) {
      javaTypeList.add("java.lang.Short");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("float")) {
      javaTypeList.add("java.lang.Float");
      javaTypeList.add("float");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("double")) {
      javaTypeList.add("java.lang.Double");
      javaTypeList.add("double");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("big_decimal")) {
      javaTypeList.add("java.math.BigDecimal");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("character")) {
      javaTypeList.add("java.lang.String");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("string")) {
      javaTypeList.add("java.lang.String");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("byte")) {
      javaTypeList.add("java.lang.Byte");
      javaTypeList.add("byte");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("boolean")) {
      javaTypeList.add("java.lang.Boolean");
      javaTypeList.add("boolean");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("yes_no")) {
      javaTypeList.add("boolean");
      javaTypeList.add("java.lang.Boolean");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("true_false")) {
      javaTypeList.add("boolean");
      javaTypeList.add("java.lang.Boolean");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("date")) {
      javaTypeList.add("java.util.Date");
      javaTypeList.add("java.sql.Date");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("time")) {
      javaTypeList.add("java.util.Date");
      javaTypeList.add("java.sql.Time");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("timestamp")) {
      javaTypeList.add("java.util.Date");
      javaTypeList.add("java.sql.Timestamp");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("calendar")) {
      javaTypeList.add("java.util.Calendar");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("calendar_date")) {
      javaTypeList.add("java.util.Calendar");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("binary")) {
      javaTypeList.add("byte[]");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("text")) {
      javaTypeList.add("java.lang.String");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("serializable")) {
      javaTypeList.add("java.io.Serializable");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("clob")) {
      javaTypeList.add("java.sql.Clob");
      javaTypeList.add("java.lang.String");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("blob")) {
      javaTypeList.add("java.sql.Blob");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("class")) {
      javaTypeList.add("java.lang.Class");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("locale")) {
      javaTypeList.add("java.util.Locale");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("timezone")) {
      javaTypeList.add("java.util.TimeZone");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    else if (textHbmType.equals("currency")) {
      javaTypeList.add("java.util.Currency");
      textHbmTypeToJavaTypeMap.put(textHbmType, javaTypeList);
      return javaTypeList;
    }
    return javaTypeList;
  }

  /**
   * @功能描述: 根据hbmType名称，返回可转换的java类型列表
   * @param textHbmType
   * @return
   * @异常类型:
   */
  public static Type transHbmType(String textHbmType) {
    if (textHbmTypeToHbmTypeMap.containsKey(textHbmType)) {
      return textHbmTypeToHbmTypeMap.get(textHbmType);
    }
    if (textHbmType.equals("integer")||textHbmType.equals("int")) {
      textHbmTypeToHbmTypeMap.put("integer", Hibernate.INTEGER);
      textHbmTypeToHbmTypeMap.put("int", Hibernate.INTEGER);
    }else if(textHbmType.equals("biginteger")){
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BIG_INTEGER);
    }
    else if (textHbmType.equals("long")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.LONG);
    }
    else if (textHbmType.equals("short")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.SHORT);
    }
    else if (textHbmType.equals("float")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.FLOAT);
    }
    else if (textHbmType.equals("double")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.DOUBLE);
    }
    else if (textHbmType.equals("big_decimal")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BIG_DECIMAL);
    }
    else if (textHbmType.equals("character")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CHARACTER);
    }
    else if (textHbmType.equals("string")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.STRING);
    }
    else if (textHbmType.equals("byte")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BYTE);
    }
    else if (textHbmType.equals("boolean")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BOOLEAN);
    }
    else if (textHbmType.equals("yes_no")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.YES_NO);
    }
    else if (textHbmType.equals("true_false")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.TRUE_FALSE);
    }
    else if (textHbmType.equals("date")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.DATE);
    }
    else if (textHbmType.equals("time")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.TIME);
    }
    else if (textHbmType.equals("timestamp")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.TIMESTAMP);
    }
    else if (textHbmType.equals("calendar")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CALENDAR);
    }
    else if (textHbmType.equals("calendar_date")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CALENDAR_DATE);
    }
    else if (textHbmType.equals("binary")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BINARY);
    }
    else if (textHbmType.equals("text")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.TEXT);
    }
    else if (textHbmType.equals("serializable")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.SERIALIZABLE);
    }
    else if (textHbmType.equals("clob")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CLOB);
    }
    else if (textHbmType.equals("blob")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.BLOB);
    }
    else if (textHbmType.equals("class")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CLASS);
    }
    else if (textHbmType.equals("locale")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.LOCALE);
    }
    else if (textHbmType.equals("timezone")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.TIMEZONE);
    }
    else if (textHbmType.equals("currency")) {
      textHbmTypeToHbmTypeMap.put(textHbmType, Hibernate.CURRENCY);
    }
    return textHbmTypeToHbmTypeMap.get(textHbmType);
  }
  
  /**
   * 
   * @功能描述: 根据java类型，自动转hibernate类型
   * @param javaType
   * @return
   * @异常类型:
   */
  public static Type javaTypeToHbmType(Class javaType)
  {
    if (javaType == Boolean.class)
    {
      return Hibernate.BOOLEAN;
    } else if (javaType == boolean.class)
    {
      return Hibernate.BOOLEAN;
    } else if (javaType == Short.class)
    {
      return Hibernate.SHORT;
    } else if (javaType == short.class)
    {
      return Hibernate.SHORT;
    } else if (javaType == Integer.class)
    {
      return Hibernate.INTEGER;
    } else if (javaType == int.class)
    {
      return Hibernate.INTEGER;
    } else if (javaType == Long.class)
    {
      return Hibernate.LONG;
    } else if (javaType == long.class)
    {
      return Hibernate.LONG;
    } else if (javaType == Double.class)
    {
      return Hibernate.DOUBLE;
    } else if (javaType == double.class)
    {
      return Hibernate.DOUBLE;
    } else if (javaType == Float.class)
    {
      return Hibernate.FLOAT;
    } else if (javaType == float.class)
    {
      return Hibernate.FLOAT;
    } else if (javaType == Date.class)
    {
      return Hibernate.TIMESTAMP;
    } else if (javaType == String.class)
    {
      return Hibernate.STRING;
    }else if(javaType==BigDecimal.class)
    {
      return Hibernate.BIG_DECIMAL;
    }else if(javaType==BigInteger.class)
    {
      return Hibernate.BIG_INTEGER;
    }else if(javaType==Byte.class)
    {
      return Hibernate.BYTE;
    }else if(javaType==java.sql.Time.class)
    {
      return Hibernate.TIME;
    }else if(javaType==java.sql.Timestamp.class)
    {
      return Hibernate.TIMESTAMP;
    }else if(javaType==java.util.Calendar.class)
    {
      return Hibernate.CALENDAR;
    }else if(javaType==byte[].class)
    {
      return Hibernate.BINARY;
    }else if(javaType==java.sql.Clob.class)
    {
      return Hibernate.CLOB;
    }else if(javaType==java.sql.Blob.class)
    {
      return Hibernate.BLOB;
    }else if(javaType==java.util.Locale.class)
    {
      return Hibernate.LOCALE;
    }else if(javaType==java.util.TimeZone.class)
    {
      return Hibernate.TIMEZONE;
    }else if(javaType==java.util.Currency.class)
    {
      return Hibernate.CURRENCY;
    }
    return Hibernate.STRING;
  }
}
